#!/bin/bash

AUTO_LOGIN_DIR=~/ssha

function init(){
    if [ -f "${AUTO_LOGIN_DIR}/servers" ];then
        echo "已经初始化过! 无需再次初始化!" && exit 1
    else
        if [ ! -d $AUTO_LOGIN_DIR ]; then
            mkdir ~/ssha
        fi
        touch $AUTO_LOGIN_DIR/servers
        echo "初始化成功! 请加入服务器信息. 🙂" && exit 1
    fi
}

declare -a server_list=()

if [ -f "$AUTO_LOGIN_DIR/servers" ];then
    while read ip user password port name; do
		if [[ -n "$name" ]] && [[ "$name" != "name" ]];then
			server_list[${name}]="${ip} ${user} ${password} ${port}"
		fi
    done < "$AUTO_LOGIN_DIR/servers"  
else
    init
fi


# 查询列表,不带密码
function list() {
    echo -e "Ip\t\t\tPort\tUser\t\tName"
    for key in ${!server_list[@]}; do
        servers=(${server_list[$key]})
        echo -e "${servers[0]}\t\t${servers[3]}\t${servers[1]}\t\t${key}"
		#| toilet -f term -F border -t --gay
    done
    exit 0
}

# 查询列表,带密码
function listPwd() {
    echo -e "Ip\t\t\tPort\tUser\t\tPassword\t\t\tName"
    for key in ${!server_list[@]}; do
        servers=(${server_list[$key]})
        echo -e "${servers[0]}\t\t${servers[3]}\t${servers[1]}\t\t${servers[2]}\t\t\t${key}"
		#| toilet -f term -F border -t --gay
    done
    exit 0
}

# 展示可用的命令
function usage() {
    echo "usage:"
    echo "ssha [-h] [-l] [-p] [-f] [-a] [-s <server alias>] [-d <server alias>]"
    exit 0
}

# 登录服务器
function login(){
    server=(${server_list[$1]})
    if [ ! $server ]; then
        echo "找不到服务" && list && exit 1
    fi
    echo "$USER logging into the【${server[0]}】 server"
	#| toilet -f term -F border --gay
    export PASSWORD=${server[2]};

	command="
        expect {
                \"*assword\" {set timeout 6000; send \$env(PASSWORD)\n; exp_continue ; sleep 3; }
                \"*passphrase\" {set timeout 6000; send \$env(PASSWORD)\r\n\"; exp_continue ; sleep 3; }
                \"yes/no\" {send \"yes\n\"; exp_continue;}
                \"Last*\" {  send_user \"\nsuccessfully logined 【${server[0]}】\n\";}
        }
       interact
    ";
   pem=${server[2]}
   if [ -f "$pem" ]
   then
	expect -c "
		spawn ssh -p ${server[3]} -i ${server[2]} ${server[1]}@${server[0]}
		${command}
	"
   else
	expect -c "
		spawn ssh -p ${server[3]} ${server[1]}@${server[0]}
		${command}
	"
   fi
    echo -e "\t$USER logged out the【${server[0]}】 server\t" 
	#| toilet -f term -F border --gay
}

# 添加服务器节点
function add(){
    read -p "Please enter servername: " servername

    servers=(${server_list[$servername]})
    if [ $servers ]; then
        echo "该服务已存在, 请更换服务名称. "
        echo -e "Ip\t\t\tPort\tUser\t\tName"
        echo -e "${servers[0]}\t${servers[3]}\t${servers[1]}\t${servername}"
		#| toilet -f term -F border -t --gay && exit 1
    fi

    read -p "Please enter ip: " ip
    read -p "Please enter username: " username
    read -p "Please enter password: " password
    read -p "Please enter port: " port

    echo -e "${ip}\t\t${username}\t\t${password}\t\t${port}\t\t${servername}" >> $AUTO_LOGIN_DIR/servers
}

# 查找servers文件目录
function findDir() {
    echo $AUTO_LOGIN_DIR
}

# 删除服务器节点
function delete() {
    server=(${server_list[$1]})
    if [ ! $server ]; then
        echo "删除的节点不存在" && exit 1
    fi
    read -p "是否确认删除该服务器节点[$1]: y/n ?" answer
    if [ $answer = 'y' ]; then
        echo '确认删除'
        unset server_list[$1]
        num=1
        if [ -f "$AUTO_LOGIN_DIR/servers" ];then
        while read ip user password port name ;do
            if [ $name = $1 ]; then
                break;
            fi
            num=$(($num+1))
        done < "$AUTO_LOGIN_DIR/servers"  
        fi
        sed -i -e "$num"d $AUTO_LOGIN_DIR/servers && echo "删除成功~" && exit 1
    elif [ $answer = 'n' ]; then
        echo '取消删除'
    else
        echo '请输入正确命令: [y/n] '
    fi

}

# clear screen
#clear
while getopts hlpaifs:d: ARGS  
do  
case $ARGS in   
    s)
        login $OPTARG
        ;;
    d)
        delete $OPTARG
        ;;
    a)
        add
        ;;
    l)
        list
        ;;
    f)
        findDir
        ;;
    p)
        listPwd
        ;;
    i)
        init
        ;;
    h)  
        usage
        ;;  
    *)  
        usage
        ;;
esac
done
